package malitech.yadam.gui;

import malitech.yadam.gui.acquire.AcquireMainWidget;
import malitech.yadam.gui.acquire.CreateJobDialog;

import com.trolltech.qt.core.QFile;
import com.trolltech.qt.designer.QUiLoader;
import com.trolltech.qt.gui.QAction;
import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QDialog;
import com.trolltech.qt.gui.QMainWindow;
import com.trolltech.qt.gui.QMenu;
import com.trolltech.qt.gui.QMessageBox;
import com.trolltech.qt.gui.QWidget;

public class MainWindow extends QMainWindow {

	private QMenu fileMenu;
	private QMenu helpMenu;

	private QAction exitAct;
	private QAction aboutAct;
	private QAction aboutQtJambiAct;
	private QAction createJobParametersAct;

	private QAction optionAct;

	public static void main(String[] args) {
		QApplication.initialize(args);

		MainWindow testMainWindow = new MainWindow(null);
		testMainWindow.show();

		QApplication.exec();
	}

	public MainWindow(QWidget parent) {
		super(parent);
		createActions();
		createMenus();
		AcquireMainWidget aquireMainWidget = new AcquireMainWidget(parent);
		this.setCentralWidget(aquireMainWidget);
	}

	private void createActions() {
		exitAct = new QAction(tr("E&xit"), this);
		exitAct.setShortcut(tr("Ctrl+Q"));
		exitAct.setStatusTip(tr("Exit the application"));
		exitAct.triggered.connect(this, "close()");

		aboutAct = new QAction(tr("&About"), this);
		aboutAct.setStatusTip(tr("Show the application's About box"));
		aboutAct.triggered.connect(this, "about()");
		optionAct = new QAction(tr("&Options"), this);
		optionAct.setStatusTip("Show application options");
		optionAct.triggered.connect(this, "option()");

		aboutQtJambiAct = new QAction(tr("About &Qt Jambi"), this);
		aboutQtJambiAct.setStatusTip(tr("Show the Qt Jambi's About box"));
		aboutQtJambiAct.triggered.connect(QApplication.instance(),
				"aboutQtJambi()");

		optionAct = new QAction(tr("&Options"), this);
		optionAct.setStatusTip("Show application options");
		optionAct.triggered.connect(this, "option()");

		createJobParametersAct = new QAction(tr("Create Job"), this);
		createJobParametersAct.setStatusTip("Create a new job");
		createJobParametersAct.triggered.connect(this, "createJob()");
	}

	private void createMenus() {
		fileMenu = menuBar().addMenu(tr("&File"));
		fileMenu.addAction(createJobParametersAct);
		fileMenu.addAction(optionAct);
		fileMenu.addAction(exitAct);

		helpMenu = menuBar().addMenu(tr("&Help"));
		helpMenu.addAction(aboutAct);
		helpMenu.addAction(aboutQtJambiAct);
	}

	protected void about() {
		QMessageBox.information(this, "Info", "It's your turn now :-)");
	}

	@SuppressWarnings("unused")
	private void option() {
		String uiResourceName = "classpath:/malitech/iman/gui/OptionsDialogBox.jui";
		QFile uiFile = new QFile(uiResourceName);
		try {
			QDialog options = (QDialog) QUiLoader.load(uiFile);
			System.out.println("Loaded");
			options.exec();
		} catch (Exception e) {
			e.printStackTrace(System.out);

		}
	}

	@SuppressWarnings("unused")
	private void createJob() {
		CreateJobDialog createJobDialog = new CreateJobDialog(this);
		createJobDialog.exec();
	}

}
